package com.niit.bc.servlet;

import com.niit.bc.bean.OrderBean;
import com.niit.bc.biz.OrderBiz;
import com.niit.bc.biz.impl.OrderBizImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class OrderSearch extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public OrderSearch() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     * <p>
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        request.setCharacterEncoding("utf8");
        response.setCharacterEncoding("utf8");
        String s_userId = request.getParameter("userId");

        if (s_userId != null) {
            int userId = Integer.parseInt(s_userId);
            OrderBiz orderBiz = new OrderBizImpl();
            List <OrderBean> orderList = orderBiz.searchOrder(userId);
            double price = 0;
            for (int i = 0; i < orderList.size(); i++) {
                OrderBean o = orderList.get(i);
                price += o.getOrderPrice();
            }
            request.setAttribute("orderList", orderList);
            request.setAttribute("price", price);
            request.setAttribute("size", orderList.size());
            request.getRequestDispatcher("orderSearch.jsp").forward(request, response);
        }
    }

    /**
     * The doPost method of the servlet. <br>
     * <p>
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}
